package ai;

import static de.dqi.pong.engine.Direction.*;
import de.dqi.pong.engine.AI;
import de.dqi.pong.engine.Tick;

/**
 * TODO Bouncer - finish class definition
 * <hr>
 * 
 * <hr><dl><dt>
 * Summary: </dt><br><dd>
 *  
 * </dd><br><dt>
 * Author: </dt><br><dd>
 *  Vincent
 * </dd></dl>
 */
public class Bouncer
		extends AI {
	
	private boolean	running;
	private long	currentTime;
	private long	lastTime;
	private double	y;
	private boolean	moveUp	= true;
	private int		fps		= Tick.fps;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		direction = Up;
		running = true;
		while (running) {
			currentTime = System.currentTimeMillis();
			
			
			if (isLeft) {
				y = tick.posBatLeft;
			} else {
				y = tick.posBatRight;
			}
			
			if (moveUp) {
				if (y > init.fieldHeight - init.batSpan - 1) {
					direction = Down;
					moveUp = false;
				}
			} else if (y < init.batSpan + 1) {
				direction = Up;
				moveUp = true;
			}
			
			try {
				long timeout = (1000 / fps) - (System.currentTimeMillis() - currentTime);
				if (timeout > 0) {
					Thread.sleep(timeout);
				}
			} catch (InterruptedException e) {
				System.err.println("Wait interval interrupted");
			}
			lastTime = currentTime;
		}
		
	}
}
